﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Windows.Forms;

namespace RuleCode.Common
{
    public class VirtualRelation
    {
        private string relationName;
        private string[] keyStringList;
        private System.Type parentTableType;
        private System.Type childTableType;

        #region 构造函数
        public VirtualRelation() { }
        public VirtualRelation(string rName, string[] keyList, System.Type parTableType, System.Type chiTableType)
        {
            if (this.Validate(keyList, parTableType, chiTableType) == false)
            { return; }

            this.childTableType = chiTableType;
            this.parentTableType = parTableType;
            this.keyStringList = keyList;
            this.relationName = rName;
        }
        #endregion

        #region 属性
        /// <summary>
        /// 获取关系名
        /// </summary>
        public string RelationName
        { get { return this.relationName; } }

        /// <summary>
        /// 获取关系约束字段名集合
        /// </summary>
        public string[] KeyListString
        { get { return this.keyStringList; } }

        /// <summary>
        /// 获取父表的表类型 是CustomerTable or OrderTable 。。。
        /// </summary>
        public System.Type ParentTableType
        { get { return this.parentTableType; } }

        /// <summary>
        /// 获取子表的表类型 是CustomerTable or OrderTable 。。。
        /// </summary>
        public System.Type ChildTableType
        { get { return this.childTableType; } }
        #endregion



        #region 验证函数Validate(string[] keyList, System.Type parTableType, System.Type chiTableType)
        /// <summary>
        /// 验证构造函数的初始化值中的关系是否存在
        /// </summary>
        /// <param name="keyList"></param>
        /// <param name="parTableType"></param>
        /// <param name="chiTableType"></param>
        /// <returns></returns>
        public bool Validate(string[] keyList, System.Type parTableType, System.Type chiTableType)
        {
            try
            {
                FieldAttribute[] attribute;
                foreach (string caption in keyList)
                {

                    if (parTableType.GetProperty(caption) == null || chiTableType.GetProperty(caption) == null)
                        throw new InvalidConstraintException("您正在尝试创建的关系不存在！！");
                    //只有一个属性FieldAttribute
                    attribute = (FieldAttribute[])(parTableType.GetProperty(caption).GetCustomAttributes(typeof(FieldAttribute), true));
                    if (attribute[0].FieldCate == FieldCategory.OTHERS || attribute[0].FieldCate == FieldCategory.VIRTUAL)
                        throw new InvalidConstraintException("您正在尝试创建的关系不存在！！");

                    attribute = (FieldAttribute[])(chiTableType.GetProperty(caption).GetCustomAttributes(typeof(FieldAttribute), true));
                    if (attribute[0].FieldCate == FieldCategory.OTHERS || attribute[0].FieldCate == FieldCategory.VIRTUAL)
                        throw new InvalidConstraintException("您正在尝试创建的关系不存在！！");
                }
            }
            catch (InvalidConstraintException e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
            return true;
        }
        #endregion
    }
}
